/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lhwalq.books.ui.fragment;

import com.lhwalq.books.base.BaseRVFragment;
import com.lhwalq.books.bean.BookToc;
import com.lhwalq.books.bean.Recommend;
import com.lhwalq.books.bean.support.DownloadQueue;
import com.lhwalq.books.bean.support.RefreshCollectionListEvent;
import com.lhwalq.books.component.AppComponent;
import com.lhwalq.books.component.DaggerMainComponent;
import com.lhwalq.books.manager.CollectionsManager;
import com.lhwalq.books.R;
import com.lhwalq.books.service.DownloadBookService;
import com.lhwalq.books.ui.activity.ReadActivity;
import com.lhwalq.books.ui.contract.RecommendContract;
import com.lhwalq.books.ui.easyadapter.RecommendAdapter;
import com.lhwalq.books.ui.presenter.RecommendPresenter;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class RecommendFragment extends BaseRVFragment<RecommendPresenter, Recommend.RecommendBooks> implements RecommendContract.View {

    private List<BookToc.mixToc.Chapters> chaptersList = new ArrayList<>();

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_recommend;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMainComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void initDatas() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void configViews() {
        initAdapter(RecommendAdapter.class, false, false);
        onRefresh();
    }

    @Override
    public void showRecommendList(List<Recommend.RecommendBooks> list) {
        mAdapter.clear();
        mAdapter.addAll(list);
        //推荐列表默认加入收藏
        for (Recommend.RecommendBooks bean : list) {
            //TODO 此处可优化：批量加入收藏->加入前需先判断是否收藏过
            CollectionsManager.getInstance().add(bean);
        }
    }

    @Override
    public void showBookToc(String bookId, List<BookToc.mixToc.Chapters> list) {
        chaptersList.clear();
        chaptersList.addAll(list);
        DownloadBookService.post(new DownloadQueue(bookId, list, 1, list.size()));
        dismissDialog();
    }

    @Override
    public void onItemClick(int position) {
        ReadActivity.startActivity(activity, mAdapter.getItem(position), mAdapter.getItem(position).isFromSD);
    }

    /**
     * 显示长按对话框
     *
     * @param position
     */
//    private void showLongClickDialog(final int position) {
//        final boolean isTop = CollectionsManager.getInstance().isTop(mAdapter.getItem(position)._id);
//        String[] items;
//        DialogInterface.OnClickListener listener;
//        if (mAdapter.getItem(position).isFromSD) {
//            items = getResources().getStringArray(R.array.recommend_item_long_click_choice_local);
//            listener = new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    switch (which) {
//                        case 0:
//                            //置顶、取消置顶
//                            CollectionsManager.getInstance().top(mAdapter.getItem(position)._id, !isTop);
//                            break;
//                        case 1:
//                            //删除
//                            List<Recommend.RecommendBooks> removeList = new ArrayList<>();
//                            removeList.add(mAdapter.getItem(position));
//                            showDeleteCacheDialog(removeList);
//                            break;
//                        case 2:
//                            //批量管理
//                            showBatchManagementLayout();
//                            break;
//                        default:
//                            break;
//                    }
//                    dialog.dismiss();
//                }
//            };
//        } else {
//            items = getResources().getStringArray(R.array.recommend_item_long_click_choice);
//            listener = new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    switch (which) {
//                        case 0:
//                            //置顶、取消置顶
//                            CollectionsManager.getInstance().top(mAdapter.getItem(position)._id, !isTop);
//                            break;
//                        case 1:
//                            //书籍详情
//                            BookDetailActivity.startActivity(activity,
//                                    mAdapter.getItem(position)._id);
//                            break;
//                        case 2:
//                            //移入养肥区
//                            ToastUtils.showToast("正在拼命开发中...");
//                            break;
//                        case 3:
//                            //缓存全本
//                            if (mAdapter.getItem(position).isFromSD) {
//                                ToastUtils.showSingleToast("本地文件不支持该选项哦");
//                            } else {
//                                showDialog();
//                                mPresenter.getTocList(mAdapter.getItem(position)._id);
//                            }
//                            break;
//                        case 4:
//                            //删除
//                            List<Recommend.RecommendBooks> removeList = new ArrayList<>();
//                            removeList.add(mAdapter.getItem(position));
//                            showDeleteCacheDialog(removeList);
//                            break;
//                        case 5:
//                            //批量管理
//                            showBatchManagementLayout();
//                            break;
//                        default:
//                            break;
//                    }
//                    dialog.dismiss();
//                }
//            };
//        }
//        if (isTop) items[0] = getString(R.string.cancle_top);
//        new AlertDialog.Builder(activity)
//                .setTitle(mAdapter.getItem(position).title)
//                .setItems(items, listener)
//                .setNegativeButton(null, null)
//                .create().show();
//    }

    /**
     * 显示删除本地缓存对话框
     *
     * @param removeList
     */
//    private void showDeleteCacheDialog(final List<Recommend.RecommendBooks> removeList) {
//        final boolean selected[] = {true};
//        new AlertDialog.Builder(activity)
//                .setTitle(activity.getString(R.string.remove_selected_book))
//                .setMultiChoiceItems(new String[]{activity.getString(R.string.delete_local_cache)}, selected,
//                        new DialogInterface.OnMultiChoiceClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//                                selected[0] = isChecked;
//                            }
//                        })
//                .setPositiveButton(activity.getString(R.string.confirm), new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        dialog.dismiss();
//                        new AsyncTask<String, String, String>() {
//                            @Override
//                            protected void onPreExecute() {
//                                super.onPreExecute();
//                                showDialog();
//                            }
//
//                            @Override
//                            protected String doInBackground(String... params) {
//                                CollectionsManager.getInstance().removeSome(removeList, selected[0]);
//                                return null;
//                            }
//
//                            @Override
//                            protected void onPostExecute(String s) {
//                                super.onPostExecute(s);
//                                ToastUtils.showSingleToast("成功移除书籍");
//                                for (Recommend.RecommendBooks bean : removeList) {
//                                    mAdapter.remove(bean);
//                                }
////                                if (isVisible(llBatchManagement)) {
////                                    批量管理完成后，隐藏批量管理布局并刷新页面
////                                    goneBatchManagementAndRefreshUI();
////                                }
//                                hideDialog();
//                            }
//                        }.execute();
//
//                    }
//                })
//                .setNegativeButton(activity.getString(R.string.cancel), null)
//                .create().show();
//    }

    /**
     * 隐藏批量管理布局并刷新页面
     */
    public void goneBatchManagementAndRefreshUI() {
        if (mAdapter == null) return;
//        gone(llBatchManagement);
        for (Recommend.RecommendBooks bean :
                mAdapter.getAllData()) {
            bean.showCheckBox = false;
        }
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 显示批量管理布局
     */
//    private void showBatchManagementLayout() {
////        visible(llBatchManagement);
//        for (Recommend.RecommendBooks bean : mAdapter.getAllData()) {
//            bean.showCheckBox = true;
//        }
//        mAdapter.notifyDataSetChanged();
//    }


//    @OnClick(tvSelectAll)
//    public void selectAll() {
////        isSelectAll = !isSelectAll;
////        tvSelectAll.setText(isSelectAll ? activity.getString(R.string.cancel_selected_all) : activity.getString(R.string.selected_all));
//        for (Recommend.RecommendBooks bean : mAdapter.getAllData()) {
//            bean.isSeleted = isSelectAll;
//        }
//        mAdapter.notifyDataSetChanged();
//    }

//    @OnClick(R.id.tvDelete)
//    public void delete() {
//        List<Recommend.RecommendBooks> removeList = new ArrayList<>();
//        for (Recommend.RecommendBooks bean : mAdapter.getAllData()) {
//            if (bean.isSeleted) removeList.add(bean);
//        }
//        if (removeList.isEmpty()) {
//            ToastUtils.showToast(activity.getString(R.string.has_not_selected_delete_book));
//        } else {
//            showDeleteCacheDialog(removeList);
//        }
//    }

    @Override
    public void onRefresh() {
        super.onRefresh();
//        gone(llBatchManagement);
        List<Recommend.RecommendBooks> data = CollectionsManager.getInstance().getCollectionListBySort();
        mAdapter.clear();
        mAdapter.addAll(data);
        //不加下面这句代码会导致，添加本地书籍的时候，部分书籍添加后直接崩溃
        //报错：Scrapped or attached views may not be recycled. isScrap:false isAttached:true
        mAdapter.notifyDataSetChanged();
        mRecyclerView.setRefreshing(false);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void RefreshCollectionList(RefreshCollectionListEvent event) {
        mRecyclerView.setRefreshing(true);
        onRefresh();
    }

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void UserSexChooseFinished(UserSexChooseFinishedEvent event) {
//        //首次进入APP，选择性别后，获取推荐列表
//        mPresenter.getRecommendList();
//    }

    @Override
    public void showError() {
        loaddingError();
        dismissDialog();
    }

    @Override
    public void complete() {
        mRecyclerView.setRefreshing(false);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!getUserVisibleHint()) {
            goneBatchManagementAndRefreshUI();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //这样监听返回键有个缺点就是没有拦截Activity的返回监听，如果有更优方案可以改掉
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
//        getView().setOnKeyListener(new View.OnKeyListener() {
//            @Override
//            public boolean onKey(View v, int keyCode, KeyEvent event) {
//                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
//                    if (isVisible(llBatchManagement)) {
//                        goneBatchManagementAndRefreshUI();
//                        return true;
//                    }
//                }
//                return false;
//            }
//        });
        MobclickAgent.onResume(getContext());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

//    private boolean isForeground() {
//        ActivityManager am = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
//        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
//        if (list != null && list.size() > 0) {
//            ComponentName cpn = list.get(0).topActivity;
//            if (MainActivity.class.getName().contains(cpn.getClassName())) {
//                return true;
//            }
//        }
//
//        return false;
//    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(getContext());
    }
}
